package com.hy.web;

import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hy.domain.NewType;
import com.hy.domain.News;

@Controller
public class HycsController {

	/*
	@RequestMapping("hycs")
	public String hycs(Model model, @RequestParam String type, @RequestParam(defaultValue = "1", required = false) int page) throws Exception {
		
		//String type2 = new String(type.getBytes(), "UTF-8");
		String type2 = type;
		NewType t = NewType.findNewTypesByNameLike(type2).getSingleResult();
		long count = News.countType(t);											//总数
		int length = 10;																//读取条数
		int pages = (int) (count % length > 0 ? count / length + 1 : count / length);   //总页数
		if(page < 1 || page > pages){
			return "redirect:hycs.html?type=" + type + "&page=1";
		}
		int up = page > 1 && page <= pages ? page - 1 : 1;					//上一页
		int next = page > 0 && page < pages ? page + 1 : 1;					//下一页
		int start = (page - 1) * length;
		List<News> news = News.getNews(t, start, length);
		
		model.addAttribute("type", type2);
		model.addAttribute("news", news);
		model.addAttribute("pages", pages);
		model.addAttribute("up", up);
		model.addAttribute("next", next);
		return "hycs";
	}
	*/
	
	@RequestMapping("hycs")
	public String hycs(Model model, @RequestParam long type, @RequestParam(defaultValue = "1", required = false) int page) throws Exception {
		
		//String type2 = new String(type.getBytes(), "UTF-8");
		//String type2 = type;
		NewType t = NewType.findNewType(type);
		long count = News.countType(t);											//总数
		int length = 10;																//读取条数
		int pages = (int) (count % length > 0 ? count / length + 1 : count / length);   //总页数
		
		/*
		if(page < 0 || page > pages){
			return "redirect:hycs.html?type=" + type + "&page=1";
		}
		*/
		int up = page > 1 && page <= pages ? page - 1 : 1;					//上一页
		int next = page > 0 && page < pages ? page + 1 : 1;					//下一页
		int start = (page - 1) * length;
		List<News> news = News.getNews(t, start, length);
		
		model.addAttribute("type", t);
		model.addAttribute("news", news);
		model.addAttribute("pages", pages);
		model.addAttribute("up", up);
		model.addAttribute("next", next);
		model.addAttribute("page", page);
		return "hycs";
	}
	
	@RequestMapping("hycs_xq")
	public String hycsXq(Model model, @RequestParam(defaultValue = "1", required = false) long id) {
		/*
		News news = News.findNews(id);
		if(news == null){
			return "redirect:404";
		}
		//更新点击率
		news.setHits(news.getHits() + 1);
		news.merge(); 
		
		model.addAttribute("news", news);
		model.addAttribute("up", news.getUp());
		model.addAttribute("next", news.getNext());
		return "hycs_xq";
		*/
		return "redirect:/hycs/view/" + id + ".html";
	}
	
	@RequestMapping("search")
	public String search(Model model, @RequestParam String field, @RequestParam long type, @RequestParam String content) throws Exception {
		/**/
		content = "%" + new String(content.getBytes("ISO-8859-1"), "UTF-8") + "%";
		NewType type2 = NewType.findNewType(type);
		TypedQuery<News> query = NewType.entityManager().createQuery("from News n where n.type = :type and " + field + " like :content", News.class).setHint("org.hibernate.cacheable", true);
		query.setParameter("type", type2);
		query.setParameter("content", content);
		List<News> news = query.getResultList();
		
		model.addAttribute("type", type2);
		model.addAttribute("news", news);
		model.addAttribute("pages", 1);
		model.addAttribute("up", 1);
		model.addAttribute("next", 1);
		return "hycs";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
